//
//  GAOComposeToolBar.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit


enum ToolBarButtonType: Int {
    //  图片
    case Picture = 0
    //  @
    case Mention = 1
    //  话题
    case Trend = 2
    //  表情
    case Emoticon = 3
    //  add
    case Add = 4
    
}

@available(iOS 9.0, *)
class GAOComposeToolBar: UIStackView {
    
    var didSeletedButtonClosure: ((type: ToolBarButtonType) -> ())?
    
    //  表情按钮
    var emoticonButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //  指定布局方式
        axis = .Horizontal
        //  填充方式,所有子空间一样宽高
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
    }
    
    private func addChildButton(imageName: String, type: ToolBarButtonType) -> UIButton {
        let button = UIButton()
        //获取枚举的原始值,给button添加标识
        button.tag = type.rawValue
        button.setImage(UIImage(named: "\(imageName)"), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        //去掉高亮效果
        button.adjustsImageWhenHighlighted = false
        
        //点击事件
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
        addArrangedSubview(button)
        return button
    }
    
    @objc private func buttonAction(button: UIButton) {
        //转成枚举
        let type = ToolBarButtonType(rawValue: button.tag)!
        didSeletedButtonClosure?(type: type)
    }
    
    //  判断是否是表情键盘,如果是表情键盘 则显示 系统键盘的图标,否则显示表情图标
    func changeEmotionIcon(isEmotionIcon: Bool) {
        
        var imageName = "compose_emoticonbutton_background"
        //若当前为表情键盘,点击后切换为系统键盘图标
        if isEmotionIcon == true {
            imageName = "compose_keyboardbutton_background"
        }
        emoticonButton?.setImage(UIImage(named: "\(imageName)"), forState: .Normal)
        emoticonButton?.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
    }
    
}
